package com.yuerlive.rainbow.module.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.common.BaseRecycleViewAdapter;
import com.yuerlive.rainbow.entity.Banner;
import com.yuerlive.rainbow.entity.LiveInfo;
import com.yuerlive.rainbow.entity.ObjectWrapper;
import com.yuerlive.rainbow.utils.DeviceUtils;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.widget.CircleImageView;
import com.yuerlive.rainbow.widget.HeadLinesView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017/5/18.
 */

public class MainHotAdapter extends BaseRecycleViewAdapter {
    private static final String TAG = "MainHotAdapter";
    private List<ObjectWrapper> mDatas;
    private final int TYPE_BANNER = 1;
    private final int TYPE_ITEM = 0;
    private final int TYPE_FOOTER = 2;
    public static final int TYPE_HEADER = 3;
    private static final int TYPE_EMPTY = 4;


    public MainHotAdapter(Context mContext, List<ObjectWrapper> datas) {
        super(mContext);
        this.mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        L.e(TAG, "onCreateViewHolder" + viewType);
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE_EMPTY:
                holder = new EmptyHolder(mInflater.inflate(R.layout.layout_exception, parent, false));
                break;
            case TYPE_HEADER:
                holder = new HeaderHolder(mInflater.inflate(R.layout.layout_header, parent, false));
                break;
            case TYPE_BANNER:
                holder = new BannerHolder(mInflater.inflate(R.layout.layout_banner, parent, false));
                break;
            case TYPE_ITEM:
                holder = new LiveHolder(mInflater.inflate(R.layout.layout_livecard, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //     L.e(TAG,"onBindViewHolder"+position);
        switch (getItemViewType(position)) {
            case TYPE_BANNER:
                initBannerData((BannerHolder) holder, position);
                break;
            case TYPE_ITEM:
                initLiveData((LiveHolder) holder, position);
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (mDatas == null || mDatas.isEmpty()) return 1;
        if (mDatas.size() == 2 && mDatas.get(1).type == TYPE_BANNER)
            return mDatas.size() + 1;
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas == null || mDatas.isEmpty()) return TYPE_EMPTY;
        if (mDatas.size() == 2 && position == 2) return TYPE_EMPTY;
        return mDatas.get(position).type;

    }

    static class HeaderHolder extends RecyclerView.ViewHolder {
        public HeaderHolder(View itemView) {
            super(itemView);
        }
    }

    static class BannerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.banner)
        HeadLinesView banner;

        public BannerHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private void initBannerData(BannerHolder holder, int position) {
        List<Banner> banners = (List<Banner>) mDatas.get(position).object;
        holder.banner.refreshData(banners);
    }

    static class EmptyHolder extends RecyclerView.ViewHolder {
        public EmptyHolder(View itemView) {
            super(itemView);
        }
    }

    static class LiveHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imgHeader)
        CircleImageView imgHeader;
        @BindView(R.id.tvNickName)
        TextView tvNickName;
        @BindView(R.id.tvHot)
        TextView tvHot;
        @BindView(R.id.imgConver)
        ImageView imgConver;
        @BindView(R.id.imgState)
        ImageView imgState;
        @BindView(R.id.imgGame)
        ImageView imgGame;
        @BindView(R.id.liveContent)
        View content;

        public LiveHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);


        }



    }

    private void initLiveData(LiveHolder holder, int position) {
        LiveInfo liveData = (LiveInfo) mDatas.get(position).object;
        holder.tvHot.setText("" + liveData.getOnlineNum());
        holder.tvNickName.setText(liveData.getNickname());
        GlideUtil.load(mContext, liveData.getUserIcon(), holder.imgHeader);
        GlideUtil.load(mContext, liveData.getLiveIcon(), holder.imgConver);
        holder.imgState.setVisibility(View.GONE);
        if(liveData.getGameId()==1){
            holder.imgGame.setImageResource(R.drawable.game_niuniu);
        }else if (liveData.getGameId()==2){
            holder.imgGame.setImageResource(R.drawable.game_cat);
        }else if (liveData.getGameId() == 3){
            holder.imgGame.setImageResource(R.drawable.game_sg);
        }
        if (liveData.getState() ==3){
            holder.imgGame.setVisibility(View.GONE);
        }else {
            holder.imgGame.setVisibility(View.VISIBLE);
        }
        int width = DeviceUtils.deviceWidth(mContext);
        ViewGroup.LayoutParams params = holder.content.getLayoutParams();
        params.width = width;
        params.height = width;
        holder.content.setLayoutParams(params);
        holder.itemView.setOnClickListener(v -> {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, position);
        });
    }


}
